package com.wise.file.server.controller;


import com.wise.file.server.domain.ImageFile;
import com.wise.file.server.service.ImageFileService;
import com.wise.file.server.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@CrossOrigin(origins = "*",maxAge = 3600)//允许所有域名访问，需要spring4.2以上版本支持
@Controller
public class ImageController {
    @Autowired private ImageFileService fileService;

    /**
     * 访问文件上传页面
     * @return
     */
    @GetMapping("/")
    public String fileServer(){
        return "fileupload";
    }

    @GetMapping(value = "/server")
    public String index(Model model){
        model.addAttribute("files",fileService.listFiles());
        return "index";
    }

    /**
     * 处理客户端文件上传操作，保存文件到mongoDB，并将文件访问地址返回
     * @param logofile
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public String handleFileUpload(@RequestParam("logofile")MultipartFile logofile,String id){
        try {
            ImageFile f = new ImageFile(logofile.getOriginalFilename(),logofile.getContentType(),logofile.getSize(),logofile.getBytes());
            f.setMd5(MD5.getMD5(logofile.getInputStream()));
            /*--id有值为更新操作--*/
            if (id != null) f.setId(id);
            System.out.println("保存前文件的id:"+f.getId());
            fileService.saveImageFile(f);
            System.out.println("保存后文件的id:"+f.getId());
            return "http://192.168.247.129:7777/view/"+f.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    /**
     * 显示文件
     * @param id
     * @return
     */
    @GetMapping("/view/{id}")
    public ResponseEntity viewFileOnline(@PathVariable String id){
        ImageFile imageFile = fileService.findFileById(id);
        if (imageFile != null){
            return ResponseEntity
                    .ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION,"filename=\""+imageFile.getName()+"\"")
                    .header(HttpHeaders.CONTENT_TYPE,imageFile.getContentType())
                    .header(HttpHeaders.CONTENT_LENGTH,imageFile.getSize()+"")
                    .header("Connection","close")
                    .body(imageFile.getContent());
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("很抱歉，文件没有找到");
        }
    }


    /**
     * 删除文件
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/delete")
    public String removeFile(String id,Model model){
        fileService.remove(id);
        model.addAttribute("files",fileService.listFiles());
        return "index";
    }

    /**
     * 修改文件
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/update")
    public String updateFile(String id,Model model){
        model.addAttribute("file",fileService.findFileById(id));
        return "update";
    }
}
